package com.fq.myapplication;


import android.graphics.Color;

import android.os.Bundle;

import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.Fragment;

import com.fq.myapplication.fragment.FavFragment;
import com.fq.myapplication.fragment.HomeFragment;
import com.fq.myapplication.fragment.ProfileFragment;
import com.fq.myapplication.utils.ThemeManager;

public class MainActivity extends AppCompatActivity {
    private TextView homeTextView, favTextView, profileTextView;
    private final int SELECTED_COLOR = Color.parseColor("#19a7ff"); // 红色作为选中色
    private final int DEFAULT_COLOR = Color.parseColor("#808080");   // 灰色作为默认色
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        // 初始化控件
        homeTextView = findViewById(R.id.home);
        favTextView = findViewById(R.id.fav);
        profileTextView = findViewById(R.id.profile);


        // 设置默认选中首页
        updateTabSelection(R.id.home);
        // # 底部栏点击切换
        // - 1. 初始化 fragment
        switchFragment(HomeFragment.newInstance());

        // - 2. 设置底部导航栏点击事件
        final TextView[] homeTextView = {findViewById(R.id.home)};
        TextView favTextView = findViewById(R.id.fav);
        TextView profileTextView = findViewById(R.id.profile);
        homeTextView[0].setOnClickListener(v -> {
            switchFragment(HomeFragment.newInstance());
            updateTabSelection(R.id.home);
        });


        favTextView.setOnClickListener(v -> {
            switchFragment(FavFragment.newInstance());
            updateTabSelection(R.id.fav);
        });

        profileTextView.setOnClickListener(v -> {
            switchFragment(ProfileFragment.newInstance());
            updateTabSelection(R.id.profile);
        });
    }

    private void updateTabSelection(int selectedId) {
        // 重置所有tab状态
        resetAllTabs();

        // 根据选中的ID设置对应状态
        if (selectedId == R.id.home) {
            homeTextView.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.home_become, 0, 0);
            homeTextView.setTextColor(SELECTED_COLOR);
        } else if (selectedId == R.id.fav) {
            favTextView.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.fav_become, 0, 0);
            favTextView.setTextColor(SELECTED_COLOR);
        } else if (selectedId == R.id.profile) {
            profileTextView.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.my_become, 0, 0);
            profileTextView.setTextColor(SELECTED_COLOR);
        }
    }

    private void resetAllTabs() {
        // 重置首页
        homeTextView.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.home_resized, 0, 0);
        homeTextView.setTextColor(DEFAULT_COLOR);

        // 重置收藏
        favTextView.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.fav_resized, 0, 0);
        favTextView.setTextColor(DEFAULT_COLOR);

        // 重置我的
        profileTextView.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.profile_resized, 0, 0);
        profileTextView.setTextColor(DEFAULT_COLOR);
        // 加载主题设置
        int themeMode = ThemeManager.getThemeMode(this);
        AppCompatDelegate.setDefaultNightMode(themeMode);
        // ...
    }
    // 抽取 fragment 替换的逻辑
    private void switchFragment(Fragment fragment) {
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.container, fragment)
                .commit();
    }

}